package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hr.pojo.PayrollO;
import cn.hr.pojo.SysUser;
import cn.hr.service.DictionaryService;
import cn.hr.service.OrganizationService;
import cn.hr.service.PayrollCrewService;
import cn.hr.service.PayrollDetailService;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.PayrollOService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织工资单控制器
 *
 * @author MO
 */
@Controller
@RequestMapping("/payroll")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PAYROLL_MANAGER') or hasRole('ROLE_PAYROLL_ATTACHE')")
public class PayrollOController {
    private Logger logger = LoggerFactory.getLogger(PayrollOController.class);
    @Resource
    private PayrollOService payrollOService;
    @Resource
    private PayrollCrewService payrollCrewService;
    @Resource
    private PayrollDetailService payrollDetailService;
    @Resource
    private OrganizationService organizationService;
    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping("/openList")
    public String openList(Model model) {
        Map map = new HashMap();
        map.put("parentId", 0);
        map.put("isStart", 1);
        try {
            model.addAttribute("organizationList", organizationService.findSubdivisionByParentId(map));
            model.addAttribute("status", dictionaryService.selectAllByTypeName("AUDITSTATUS_TYPE"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payroll/list";
    }

    @RequestMapping("/addPayroll/{oId}")
    @ResponseBody
    public String addPayroll(@PathVariable("oId") String oId, HttpServletRequest request) {
        PayrollO payrollO = new PayrollO();
        payrollO.setoId(Integer.valueOf(oId));
        payrollO.setCreatedById(findIdBySession(request));
        try {
            if (payrollOService.insert(payrollO)) {
                return "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    @RequestMapping("/showList")
    public String showList(@RequestParam(required = false) Map map, @RequestParam("pageNo") String pageNo, Model model) {
        try {
            model.addAttribute("pageInfo", payrollOService.findListByCondition(map, Integer.parseInt(pageNo)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payroll/showList";
    }

    @RequestMapping("/openCrewList/{id}/{str}")
    public String openCrewList(@PathVariable("id") String poId, @PathVariable("str") String str, Model model) {
        try {
            model.addAttribute("poMap", payrollOService.findMapById(poId));
            model.addAttribute("str", str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payroll/crewList";
    }

    @RequestMapping("/showCrewList")
    public String showCrewList(@RequestParam("poId") String poId,
                               @RequestParam("str") String str, @RequestParam("pageNo") String pageNo, Model model) {
        try {
            Map map = new HashMap();
            map.put("poId", poId);
            PageInfo<Map> pageInfo = payrollCrewService.findListByCondition(map, Integer.parseInt(pageNo));
            model.addAttribute("headSet", pageInfo.getList().remove(pageInfo.getList().size() - 1).get("headSet"));
            model.addAttribute("pageInfo", pageInfo);
            PayrollO payrollO = payrollOService.get(Integer.valueOf(poId));
            model.addAttribute("poId", payrollO.getId());
            model.addAttribute("poCode", payrollO.getCode());
            model.addAttribute("auditStatus", payrollO.getAuditStatus());
            model.addAttribute("auditOpinion", payrollO.getAuditOpinion());
            model.addAttribute("crewPoTotalMoney", payrollO.getTotalMoney());
            model.addAttribute("str", str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payroll/showCrewList";
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable("id") String id) {
        try {
            if (!payrollOService.delete(Integer.valueOf(id))) {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "true";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(PayrollO payrollO, HttpServletRequest request) {
        payrollO.setModifyById(findIdBySession(request));
        try {
            if (!payrollOService.update(payrollO)) {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "true";
    }

    public Integer findIdBySession(HttpServletRequest request) {
        return ((SysUser) request.getSession().getAttribute(Constants.USER_SESSION)).getId();
    }
}
